package sn.gandal.agriculture.agrimobile.j2me.gui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

/**
 *
 * @author dysow
 */
public class ProgressForm extends Form implements CommandListener
{

    private Displayable previousScreen;
    private Display display;
    private StringItem strAction;
   //private Command cmdCancel;
    private Thread thread;

    public ProgressForm(String title, String action, Displayable previousScreen, Display display)
    {
	super(title);

	this.previousScreen = previousScreen;
	this.display = display;

	this.initialiseComponents(action);
	this.setLayout();
	this.setCommandListener(this);
    }

    protected void initialiseComponents(String action)
    {
	this.strAction = new StringItem("", action);

	//this.cmdCancel = new Command("Cancel", Command.CANCEL, 1);
    }

    protected void setLayout()
    {
	this.append(this.strAction);

	//this.addCommand(this.cmdCancel);
    }

    public void setThread(Thread th)
    {
	this.thread = th;
    }

//    public boolean equalsCmdCancel(Command c)
//    {
//	return this.cmdCancel.equals(c);
//    }

    public void cancelAction()
    {
	this.display.setCurrent(this.previousScreen);

	try
	{
//	    this.thread.interrupt();
	    this.thread.notify();
	}
	catch (Exception e)
	{
	}
    }

    public void activatePreviousScreen()
    {
	this.display.setCurrent(this.previousScreen);

	try
	{
	    this.thread.join();
	}
	catch (InterruptedException ex)
	{
//	    ex.printStackTrace();//debug
	}
    }

    public void commandAction(Command c, Displayable d)
    {
//	if (this.equalsCmdCancel(c))
//	{
//	    this.cancelAction();
//	}
    }
}
